package com.hzh.cusviewdemo.Adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hzh.cusviewdemo.Bean.TypeData;
import com.hzh.cusviewdemo.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LeftListAdapter extends RecyclerView.Adapter<LeftListAdapter.MyHolder> {

    private List<TypeData> dataList;

    private int nowPosition;

    private OnTypeListener mListener;

    public interface OnTypeListener {
        void onClickType(int pos);
    }

    public void setOnTypeListener(OnTypeListener listener) {
        mListener = listener;
    }

    public LeftListAdapter(List<TypeData> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public LeftListAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose_left, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LeftListAdapter.MyHolder holder, int position) {
        holder.icl_type.setText(dataList.get(position).getTitle());
        if (nowPosition == position) {
            holder.icl_linear.setVisibility(View.VISIBLE);
            holder.icl_type.setTextColor(Color.parseColor("#FD6A30"));
        } else {
            holder.icl_linear.setVisibility(View.GONE);
            holder.icl_type.setTextColor(Color.parseColor("#000000"));
        }
        holder.icl_linear_all.setOnClickListener(v -> {
            nowPosition = holder.getAdapterPosition();
            // 将点击的内容回调到外层
            mListener.onClickType(position);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        private LinearLayout icl_linear;
        private LinearLayout icl_linear_all;
        private TextView icl_type;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            icl_type = itemView.findViewById(R.id.icl_type);
            icl_linear = itemView.findViewById(R.id.icl_linear);
            icl_linear_all = itemView.findViewById(R.id.icl_linear_all);
        }
    }
}
